package com.rental.web.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rental.common.exception.ProjectException;
import com.rental.common.result.ResultCodeEnum;
import com.rental.houses.client.SubletHouseFeignClient;
import com.rental.model.form.house.SubletHouseAddForm;
import com.rental.model.form.house.SubletHousePageForm;
import com.rental.model.vo.house.SubletHouseInfoVo;
import com.rental.model.vo.house.SubletHousePageVo;
import com.rental.quickenPoint.client.ChargeRecordClient;
import com.rental.quickenPoint.client.UserAccountClient;
import com.rental.web.service.SubletHouseService;
import com.rental.common.util.AjaxResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SubletHouseServiceImpl implements SubletHouseService {
    @Autowired
    private SubletHouseFeignClient subletHouseFeignClient;

    @Autowired
    private UserAccountClient userAccountClient;

    /**
     * 转租房屋页面
     *
     * @param page                当前页码
     * @param size                每页条数
     * @param subletHousePageForm 查询条件
     * @return 转租房屋页面
     */
    @Override
    public Page<SubletHousePageVo> getSubletHousePage(Integer page, Integer size, SubletHousePageForm subletHousePageForm) {
        return AjaxResultHandler.handle(subletHouseFeignClient.getSubletHousePage(page, size, subletHousePageForm));
    }

    /**
     * 转租房屋详情
     *
     * @param id 转租屋id
     * @return 转租屋详情
     */
    @Override
    public SubletHouseInfoVo getSubletHouseInfoById(Long id, Long uid) {

        return AjaxResultHandler.handle(subletHouseFeignClient.getSubletHouseInfoById(id, uid));
    }

    /**
     * 发布转租房屋
     *
     * @param subletHouseAddForm 转租房屋信息
     * @return 发布结果
     */
    @Override
    public Boolean addSubletHouse(SubletHouseAddForm subletHouseAddForm) {
        // 增加加速值
        if (!AjaxResultHandler.handle(userAccountClient.sendAccelerateValueByAddInfo(subletHouseAddForm.getUid())))
            throw new ProjectException(ResultCodeEnum.QUICKEN_ADD_ERROR);
        return AjaxResultHandler.handle(subletHouseFeignClient.addSubletHouse(subletHouseAddForm));
    }

    /**
     * 下架房屋
     *
     * @param id  房屋id
     * @param uid 用户id
     * @return 下架结果
     */
    @Override
    public Boolean downHouse(Long id, Long uid) {

        return AjaxResultHandler.handle(subletHouseFeignClient.downHouse(id, uid));
    }

    /**
     * 获取联系微信
     *
     * @param id  房屋id
     * @param uid 用户id
     * @return 联系微信
     */
    @Autowired
    private ChargeRecordClient chargeRecordClient;
    @Override
    public String getContactWechat(Long id, Long uid) {
        Boolean result = chargeRecordClient.connectWay(uid).getData();
        if (!result) {
            throw new ProjectException(ResultCodeEnum.QUICKEN_NOT_ENOUGH);
        }
        return AjaxResultHandler.handle(subletHouseFeignClient.getContactWechat(id));
    }

    /**
     * 获取联系电话
     *
     * @param id  房屋id
     * @param uid 用户id
     * @return 联系电话
     */
    @Override
    public String getContactPhone(Long id, Long uid) {
        Boolean result = chargeRecordClient.connectWay(uid).getData();
        if (!result) {
            throw new ProjectException(ResultCodeEnum.QUICKEN_NOT_ENOUGH);
        }
        return AjaxResultHandler.handle(subletHouseFeignClient.getContactPhone(id));
    }

    /**
     * 删除房屋
     *
     * @param id  房屋id
     * @param uid 用户id
     * @return 删除结果
     */
    @Override
    public Boolean deleteSubletHouse(Long id, Long uid) {
        return AjaxResultHandler.handle(subletHouseFeignClient.deleteSubletHouse(id, uid));
    }

    /**
     * 收藏转租房屋
     *
     * @param id  房屋id
     * @param uid 用户id
     * @return 收藏结果
     */
    @Override
    public Boolean collectHouse(Long id, Long uid) {
        return AjaxResultHandler.handle(subletHouseFeignClient.collectHouse(id, uid));
    }

    /**
     * 取消收藏转租房屋
     *
     * @param id  房屋id
     * @param uid 用户id
     * @return 取消收藏结果
     */
    @Override
    public Boolean cancelCollectHouse(Long id, Long uid) {
        return AjaxResultHandler.handle(subletHouseFeignClient.cancelCollectHouse(id, uid));
    }

    /**
     * 举报转租房屋
     *
     * @param id     房屋id
     * @param userId 用户id
     * @return 举报后的房屋举报数
     */
    @Override
    public Integer accusation(Long id, Long userId) {
        return AjaxResultHandler.handle(subletHouseFeignClient.accusation(id, userId));
    }
}
